package cn.cdiothree.controller;

import cn.cdiothree.dao.OrderCommentMapper;
import cn.cdiothree.dataobject.OrderComment;
import cn.cdiothree.service.OrderCommentService;
import cn.cdiothree.util.COSClientUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author cumthing
 * @ClassName: UserController
 * @Description: ${todo}
 * @date 2020/3/28
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private OrderCommentMapper orderCommentMapper;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }


    /**
     * 订单评论上传图片
     * @param file
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile file, @RequestParam("userId") Integer userId)throws Exception{
        COSClientUtil cosClientUtil = new COSClientUtil();

        String name = cosClientUtil.uploadFile2Cos(file);
        String imgUrl = cosClientUtil.getImgUrl(name);

        QueryWrapper<OrderComment> wrapper = new QueryWrapper<OrderComment>();
        wrapper.eq("user_id",userId);
        OrderComment orderComment = orderCommentMapper.selectById(userId);
        orderComment.setCommentImgs(imgUrl);
        orderCommentMapper.updateById(orderComment);

        String[] split = imgUrl.split("\\?");
        return split[0];
    }
}
